package com.mvu.banana.common.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.FocusWidget;

/**
 * Class description...
 *
 * @author gantz
 */
public abstract class ValueDTO<T> implements IsSerializable {
  public void update(T widget){
  }

  public void prepare(T widget) {
  }

  protected void copy(T widget) {
  }

  protected boolean validate(T p0) {
    return true;
  }

  public void post(final T widget, FocusWidget trigger) {
    AsyncCallbackAdapter<ValueDTO> callbackAdapter = new AsyncCallbackAdapter<ValueDTO>(trigger) {
      public void afterUpdate(ValueDTO dto) {
        dto.update(widget);
      }
    };
    post(widget, trigger, callbackAdapter);
  }

  public void post(final T widget, FocusWidget trigger, AsyncCallback<? extends ValueDTO> callback) {
    copy(widget);
    if (validate(widget)) {
      trigger.setEnabled(false);
      EvalService.App.getInstance().getValues(this, callback);
    }
  }

  public void get(final T widget) {
    AsyncCallbackAdapter<ValueDTO> callbackAdapter = new AsyncCallbackAdapter<ValueDTO>() {
      public void afterUpdate(ValueDTO dto) {
        dto.update(widget);
      }
    };
    get(widget, callbackAdapter);
  }

  public void get(final T widget, AsyncCallback<? extends ValueDTO> callback) {
    copy(widget);
    EvalService.App.getInstance().getValues(this, callback);
  }
}
